package com.zhuss.core.processor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.user.SearchUserBo;
import com.zhuss.bean.unit.core.company.DeptBean;
import com.zhuss.bean.unit.core.role.RoleBean;
import com.zhuss.bean.vo.core.company.DetailCompanyVo;
import com.zhuss.bean.vo.core.user.DetailUserVo;
import com.zhuss.bean.vo.core.user.ListUserVo;
import com.zhuss.common.ListVo;
import com.zhuss.common.PageVo;
import com.zhuss.common.SelectVo;
import com.zhuss.core.db.dao.ext.UserDao;
import com.zhuss.core.db.entity.User;
import com.zhuss.enums.StartStopEnum;
import com.zhuss.exception.AabException;
import com.zhuss.exception.ExceptionCode;


/**
 * 查询用户 processor
 * @Author: zhushanshuo
 * @Date: 2019/9/24 19:54
 */
@Component("userRead")
public class UserRead {
	
	@Autowired
	private UserDao userDao;
	
	@Autowired
	@Qualifier("userDeptRead")
	private UserDeptRead userDeptRead;
	
	@Autowired
	@Qualifier("userRoleRead")
	private UserRoleRead userRoleRead;
	
	@Autowired
	@Qualifier("rolePermissionRead")
	private RolePermissionRead rolePermissionRead;
	
	@Autowired
	@Qualifier("permissionResourceRead")
	private PermissionResourceRead permissionResourceRead;
	
	@Autowired
	@Qualifier("resourceRead")
	private ResourceRead resourceRead;
	
	
	@Autowired
	@Qualifier("deptRead")
	private DeptRead deptRead;
	
	@Autowired
	@Qualifier("roleRead")
	private RoleRead roleRead;
	
	@Autowired
	@Qualifier("companyRead")
	private CompanyRead companyRead;
	
	@SuppressWarnings("unchecked")
	public List<Long> listHasAuthUser(Integer area, List<Long> resourceIdList) {
		
		// 查询出资源点对应的权限
		List<Long> permissionIdList = permissionResourceRead.listPermissionId(resourceIdList);
		
		// 查询出权限点对应的角色
		List<Long> roleIdList = rolePermissionRead.listRoleId(permissionIdList);
		List<Long> roleIdListArea = (List<Long>) roleRead.listRoleId(area, StartStopEnum.启用.key());
    	roleIdList.retainAll(roleIdListArea);
		
		// 查询出角色对应的用户
		List<Long> userIdList = userRoleRead.listUserIdByRoleList(roleIdList);
		
		userIdList = new ArrayList<>(new HashSet<>(userIdList));
		
		return userIdList;
	}
	
	
	/**
	 * 根据公司查询用户列表
	 * @param 
	 * @return
	 */
	public List<SelectVo> listSelectByCompany(Long companyId) {
		List<SelectVo> result = new ArrayList<SelectVo>();
		List<User> userList = userDao.listByCompany(Arrays.asList(companyId));
		for (User u : userList) {
			SelectVo sv = new SelectVo(u.getUserId().intValue(), u.getName(), "");
			result.add(sv);
		}
		return result;
	}
	
	/**
	 * 根据公司列表查询
	 * 用户Id列表
	 * @param area
	 * @return
	 */
	public Object listUserIdByCompany(List<Long> companyIdList) {
		List<User> userList = userDao.listByCompany(companyIdList);
		List<Long> result = new ArrayList<>();
		for (User user : userList) {
			result.add(user.getUserId());
		}
		return result;
	}
	
	/**
	 * 根据公司查询
	 * 用户Id列表
	 * @param area
	 * @return
	 */
	public Object listUserIdByCompany(Long companyId) {
		List<User> userList = userDao.listByCompany(Arrays.asList(companyId));
		List<Long> result = new ArrayList<>();
		for (User user : userList) {
			result.add(user.getUserId());
		}
		return result;
	}
    
	
	/**
	 * 访客
	 * 用户Id列表
	 * @return
	 */
	public Object listUserIdUnbind() {
		List<User> userList = userDao.listUnbind();
		List<Long> result = new ArrayList<>();
		for (User user : userList) {
			result.add(user.getUserId());
		}
		return result;
	}
	
	/**
	 * 用户Id列表
	 * @param area
	 * @return
	 */
	public Object listUserId(Integer area) {
		List<User> userList = userDao.listAll(area);
		List<Long> result = new ArrayList<>();
		for (User user : userList) {
			result.add(user.getUserId());
		}
		return result;
	}
	
	/**
	 * 访客
	 * 用户
	 * @return
	 */
	public Object listUserUnbind() {
		List<User> userList = userDao.listUnbind();
		List<ListVo> result = new ArrayList<>();
		transferList(userList, result);
		return result;
	}
	
	/**
	 * 用户
	 * @param area
	 * @return
	 */
	public Object listUser(Integer area) {
		List<User> userList = userDao.listAll(area);
		List<ListVo> result = new ArrayList<>();
		transferList(userList, result);
		return result;
	}
	
	private void transferList(List<User> userList, List<ListVo> result) {
		for (User item : userList) {
			ListVo vo = new ListVo();
			vo.setKey(item.getUserId());
			vo.setLabel(item.getName());
			result.add(vo);
		}
		
	}

	/**
	 * 用户列表
	 * @param bo
	 * @return
	 * @throws AabException 
	 */
	public Object list(SearchUserBo bo) throws AabException {
		PageVo<ListUserVo> result = new PageVo<ListUserVo>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
        result.setItems(transfer(userDao.list(bo)));
        result.setTotal(userDao.countList(bo));
		return result;
	}
	
	@SuppressWarnings("unchecked")
	private List<ListUserVo> transfer(List<ListUserVo> list) throws AabException {
		for (ListUserVo item : list) {
			DetailCompanyVo company = (DetailCompanyVo) companyRead.detail(item.getCompanyId());
			List<Long> deptIdList = (List<Long>) userDeptRead.listDeptId(item.getUserId());
			List<DeptBean> deptList = new ArrayList<>();
			if (!deptIdList.isEmpty()) {
				deptList = deptRead.listById(deptIdList);
			}
			
			List<Long> roleIdList = (List<Long>) userRoleRead.listRoleId(item.getUserId());
			List<RoleBean> roleList = new ArrayList<>();
			if (!roleIdList.isEmpty()) {
				roleList = roleRead.listById(roleIdList);
			}
			item.setCompanyName(company.getName());
			item.setDeptList(deptList);
			item.setRoleList(roleList);
		}
		return list;
	}



	/**
	 * 用户详情
	 * @param userId
	 * @return
	 * @throws AabException
	 */
	@SuppressWarnings("unchecked")
	public Object detail(Long userId) throws AabException {
		Object result = userDao.detail(userId);
		if (result == null) {
			throw new AabException(ExceptionCode.数据不存在);
		}
		DetailUserVo vo = (DetailUserVo) result;
		vo.setDeptIdList((List<Long>) userDeptRead.listDeptId(userId));
		vo.setRoleIdList((List<Long>) userRoleRead.listRoleId(userId));
		if (vo.getCompanyId() != null && vo.getCompanyId() > 0l) {
			vo.setCompanyName(((DetailCompanyVo) companyRead.detail(vo.getCompanyId())).getName());
		}
		return result;
	}
	
	
	public List<User> list(String username) {
		List<User> result = userDao.listByUsername(username);
		return result;
	}
	
	public List<User> listNotId(String username, Long userId) {
		List<User> result = new ArrayList<User>();
		List<User> userList = list(username);
		for (User c : userList) {
			if (!c.getUserId().equals(userId)) {
				result.add(c);
				break;
			}
		}
		return result;
	}
	
	
	public List<User> login(String username, String pwd) {
		List<User> result = userDao.listByLogin(username, pwd, StartStopEnum.启用.key());
		return result;
	}
	
	public List<User> listByOpenId(String openId) {
		List<User> result = userDao.listByOpenId(openId, StartStopEnum.启用.key());
		return result;
	}
	
	public List<Long> listByName(String name) {
		List<Long> result = userDao.listByName(name, StartStopEnum.启用.key());
		return result;
	}
	
	

}
